const express = require('express')
const router = express.Router()
const { upsertAnnouncement, getAnnouncement } = require('../services/AnnouncementService')

// 插入或更新唯一一条记录
router.put('/upsert', async (req, res) => {
  const { geojson } = req.body
  const result = await upsertAnnouncement(geojson)
  if (result[0]) {
    res.status(200).json({
      msg: '公告要素更新成功',
      code: 1,
    })
  } else {
    res.status(401).json({
      msg: '公告要素更新失败',
      code: 0,
    })
  }
})

// 获取唯一一条记录
router.get('/get', async (req, res) => {
  const result = await getAnnouncement()
  if (result) {
    res.status(200).json({
      msg: '公告要素获取成功',
      code: 1,
      data: result,
    })
  } else {
    res.status(401).json({
      msg: '公告要素获取失败',
      code: 0,
    })
  }
})

module.exports = router
